#include "globals.h"
#include "files_needed.h"
//#include "Sound.h"
#include"ManageGame.h"

//There should only be one console in use
_Console * g_console;

// the game is global, so that it can be deleted in Kill()
ManageGame g_app;

//This include is only needed for atexit
#include "stdlib.h"
//This bool is only used in Kill()
bool hasExited = false;
/**
  *This is the final function called in the program. Anything that needs to be
  *deleted or deconstructed, should be done in this function.
  */
void Kill()
{
	if(!hasExited)
	{
		hasExited = true;
		//All final delete function and deconstruction should be called here
		//app.Release();
		/* Cleanup */
		g_app.Release();
		Graphics::finalRelease();
		delete g_console;
	}
}

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	//* Has to be called first to make sure the Kill function is placed in the atexit method *//
	atexit(Kill);

	/* Declarations */
	MSG msg;	ZeroMemory(&msg, sizeof(msg));
	
	//creates window doesnt need to be mess with
	g_app.createWindow(nCmdShow);

	//init everything should be where everyones init stuff goes
	g_app.init_allEngines();

	//Threw this here for now
	// Anybody can take it and move it anywhere they want.
	// Multiple can be created and used simultaneously as long as they are not deleted
	// But it is best to only have one created
	g_console = new _Console();
	
	g_console->Print("Hello World");

	g_app.initGame();

	/* Main Game Loop */
	while(msg.message != WM_QUIT)
	{
		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			Windows_Input::Engine()->CheckMessage(msg);
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		// Input()
		g_app.Update();
// TODO	sg_app.serviceSounds();
		g_app.RenderGame();
	}

	return msg.wParam;
}
